package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqWaterProductionFailureMapper;
import com.guofen.pojo.SjqWaterProductionFailure;
import com.guofen.pojo.SjqWaterProductionFailure;
import com.guofen.service.sjq.SjqWaterProductionFailureService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqWaterProductionFailureServiceImpl implements SjqWaterProductionFailureService {
    
    @Autowired
    private SjqWaterProductionFailureMapper sjqWaterProductionFailureMapper;
    
    @Override
    public boolean addForm(String equipment_name, String failure_reason, String maintenance_method, String recovery_time, String examiner, String down_time, String operate_person, String repair_person, String failure_description) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();

        SjqWaterProductionFailure sjqWaterProductionFailure = new SjqWaterProductionFailure();
        sjqWaterProductionFailure.setDate(date);
        sjqWaterProductionFailure.setEquipmentName(equipment_name);
        sjqWaterProductionFailure.setFailureReason(failure_reason);
        sjqWaterProductionFailure.setMaintenanceMethod(maintenance_method);
        sjqWaterProductionFailure.setRecoveryTime(recovery_time);
        sjqWaterProductionFailure.setExaminer(examiner);
        sjqWaterProductionFailure.setDownTime(down_time);
        sjqWaterProductionFailure.setOperatePerson(operate_person);
        sjqWaterProductionFailure.setRepairPerson(repair_person);
        sjqWaterProductionFailure.setFailureDescription(failure_description);

        int result = sjqWaterProductionFailureMapper.insert(sjqWaterProductionFailure);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public SjqWaterProductionFailure findForm() {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println("当前日期" + date);
        //获取最新记录
        SjqWaterProductionFailure sjqWaterProductionFailure = sjqWaterProductionFailureMapper.findLast();
        System.out.println("最新记录" + sjqWaterProductionFailureMapper.findLast());
        //获取最新记录日期
        String dateLast =  sjqWaterProductionFailure.getDate();
        System.out.println("最新记录日期" + dateLast);

        if (date.equals(dateLast))
            return sjqWaterProductionFailure;
        else
            return null;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqWaterProductionFailure> list = sjqWaterProductionFailureMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqWaterProductionFailure> list = sjqWaterProductionFailureMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqWaterProductionFailureMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqWaterProductionFailure record) {
        return sjqWaterProductionFailureMapper.updateByPrimaryKeySelective(record);
    }
}
